package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import util.IdWorker;

@Service
public class ArticleSearchService {

    @Autowired
    private ArticleSearchDao articleSearchDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 添加
     * 一般搜索的数据从数据库同步
     */
    public void save(Article article) {
        article.setId(idWorker.nextId() + "");
        articleSearchDao.save(article);
    }

    /**
     * 根据关键字查询
     */
    public Page<Article> findByKey(String key, int page, int size) {

        Pageable pageable = PageRequest.of(page - 1, size);
        return articleSearchDao.findByTitleOrContentLike(key, key, pageable);
    }
}
